package com.avricot.userinfo;

public class UserInfoUtils {

    public static final String botUserAgentRegex = "(Googlebot)|(MSNBot)|(Slurp)|(grub)|(Scooter)|(GameSpy)|(Gigabot)|(nuhk)|(Yammybot)|(Openbot)|(Slurp)|(Ask Jeeves/Teoma)|(ia_archiver)|(Baiduspider)|(Exabot)";

    public static String getVersionAfterIdentifier(final String userAgent, final String identifier) {
	if (userAgent.contains(identifier)) {
	    try {
		final String part = userAgent.substring(userAgent.indexOf(identifier) + identifier.length());
		int lastSemicolon = part.indexOf(";");
		lastSemicolon = lastSemicolon == -1 ? part.length() : lastSemicolon;
		int lastSpace = part.indexOf(" ");
		lastSpace = lastSpace == -1 ? part.length() : lastSpace;
		int lastParenthesis = part.indexOf(")");
		lastParenthesis = lastParenthesis == -1 ? part.length() : lastParenthesis;
		final String version = part.substring(0, Math.min(Math.min(Math.min(part.length(), lastSpace), lastSemicolon), lastParenthesis));
		return version.equals("") ? UserInfo.VERSION_UNKNOWN : version;
	    } catch (final IndexOutOfBoundsException e) {
		return UserInfo.VERSION_UNKNOWN;
	    }
	}
	return null;
    }
}
